# -*- coding: utf-8 -*-

import unittest
import os
import codecs

from json_rounds_creater import JsonRoundsCreater
from model.gamestate import *

JSON_EXAMPLE = """{
    "game_type": "None",
    "is_paired_strategy": true,
    "pin": 1234,
    "remote_id": 123,

    "rounds": [
    {"player1": "Petya",
     "player2": "Vasya",
     "time": 25,
        "words": [
            {"owner": "Petya",
                "result": "GUESS",
                "text": "golovotyapka",
                "time": 5},
            {"owner": "Vasya",
                "result": "FAIL",
                "text": "stol",
                "time": 6}]}],
    "settings": {"skip_words_per_round": 1,
                 "time_before_out_sec": 5,
                 "time_per_round_sec": 25},
    "words_in_hat": [
        {"owner": null,
            "text": "cat"},
        {"owner": "Petya",
            "text": "dog"}]
}
"""


class JsonRoundsCreaterTest(unittest.TestCase):
    def setUp(self):
        self.rounds = []
        player1 = Player(u'Petya', [])
        player2 = Player(u'Vasya', [])
        word1 = Word(player1, u'golovotyapka')
        word2 = Word(player2, u'stol')
        player1.add_word(word1)
        player2.add_word(word2)
        words_in_hat = []
        words_in_hat.append(Word(None, u'cat'))
        words_in_hat.append(Word(player1, u'dog'))
        words = []
        words.append(WordGuessResult(word1, WordGuessResult.GUESS, 5))
        words.append(WordGuessResult(word2, WordGuessResult.FAIL, 6))
        self.hat = Hat(words_in_hat)
        self.settings = Settings(time_per_round_sec=25, time_before_out_sec=5)
        self.settings.skip_words = 1
        self.rounds.append(GameRound(0, words, None, (player1, player2)))
        self.players = Players([player1, player2])
        self.players.set_next_pair_strategy(TraditionalStrategy(
                                            self.players.players))
        self.pin = 1234

    def test_create_json_rounds(self):
        gamestate = GameState(self.players, self.hat, None, None, self.rounds,
                              None, self.settings)
        gamestate.pin_code = self.pin
        gamestate.remote_id = 123
        json_got = JsonRoundsCreater().create_json_rounds(gamestate)

        json_got = self.remove_spaces(json_got)
        json_example = self.remove_spaces(JSON_EXAMPLE)
        self.assertEqual(json_got, json_example)

    def remove_spaces(self, text):
        text = text.replace(' ', '')
        text = text.replace('\n', '')
        text = text.replace('\r', '')
        text = text.replace('\t', '')
        return text


if __name__ == "__main__":
    unittest.main()
